package me.huangxiang.design_pattern.observer;

import java.util.LinkedList;
import java.util.List;

public class WeatherData implements Subject {
    private float temp;
    private float humidity;
    private float pressure;
    private List<Observer> observers = new LinkedList<>();
    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        this.observers.remove(observer);
    }

    private void notifyObservers() {
        this.observers.forEach(observer -> observer.update(temp, humidity, pressure));
    }

    void updateMeasurement(float temp, float humidity, float pressure){
        this.temp = temp;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementChanged();
    }

    private void measurementChanged(){
        notifyObservers();
    }
}
